package ThreadDemo.MyThreadInterrupted;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-09-25
 * Time：11:32
 */
public class MyThread1 {
    public  static  boolean state = true;
    public static void main(String[] args) throws InterruptedException {

        Thread t  = new Thread(() -> {
            while(state) {
                System.out.println("hello Thread1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });


//        创建线程
            t.start();
        Thread.sleep(3000);
        state = false;

    }
}



class Thread2 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            // 先获取当前线程
            Thread currentThread = Thread.currentThread();

//            判断线程状态
            while(!currentThread.isInterrupted()) {
                System.out.println("hello thread1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        });

//        创建线程
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

//        修改线程状态
        t.interrupt();

    }
}